package chapter2;

import java.util.Scanner;

public class User {
    public static String arr1[] = new String[3];
    public static String arr2[] = new String[3];
    static int index = 0;

    public static void main(String[] args) {
        arr1[0] = "heima";
        arr2[0] = "123456";
        while (true) {
            Scanner scan = new Scanner(System.in);
            System.out.println("请选择功能: ");
            String str = scan.next();
            if (str.equals("1")) {
                login();
            } else if (str.equals("2")) {
                register();
            } else if (str.equals("3")) {
                show();
                break;
            } else if (str.equals("0")) {
                System.exit(0);
            } else {
                System.out.println("输入错误");
            }
        }
    }

    public static void login() {
        for (int i = 0; i < 3; i++) {
            Scanner scan1 = new Scanner(System.in);
            System.out.println("请输入用户名:");
            String str1 = scan1.next();
            Scanner scan2 = new Scanner(System.in);
            System.out.println("请输入密码: ");
            String str2 = scan2.next();
            for (int i1 = 0; i1 < arr2.length; i1++) {
                if (str1.equals(arr1[i1]) && str2.equals(arr2[i1])) {
                    System.out.println("登陆成功! ");
                    return;
                }
            }
            System.out.println("登陆失败,请重新输入!");
        }
    }

    public static void register() {
        Scanner scan2 = new Scanner(System.in);
        System.out.println("请输入用户名: ");
        String str1 = scan2.next();
        Scanner scan3 = new Scanner(System.in);
        System.out.println("请输入密码: ");
        String str2 = scan2.next();
        boolean flag = true;
        for (int i = 0; i < arr1.length; i++) {
            if (str1.equals(arr1[i])) {
                System.out.println("该用户已存在");
                flag = false;
            }
        }
        if (flag == true) {
            index++;
            if (index < arr1.length) {
                arr1[index] = str1;
                arr2[index] = str2;
                System.out.println("注册成功!");
            } else {
                add();
                arr1[index] = str1;
                arr2[index] = str2;
                System.out.println("注册成功!");
            }
        }
    }

    public static void show() {
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != null) {
                System.out.println("用户名: " + arr1[i] + "," + "密码: " + arr2[i] + "\n");
            }
        }
    }

    public static void add() {
        String newarray1[] = new String[arr1.length + 3];
        String newarray2[] = new String[arr1.length + 3];
        System.arraycopy(arr1, 0, newarray1, 0, arr1.length);
        System.arraycopy(arr2, 0, newarray2, 0, arr1.length);
        arr1 = newarray1;
        arr2 = newarray2;
        show();

    }
}

